package com.yanghui.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanghui.pojo.Link;
import com.yanghui.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
@RequestMapping("/admin")
@Controller
public class LinkControllerAdmin {
    @Autowired
    private LinkService linkService;


    /**
     * 查询未处理友情链接数量
     */
    @RequestMapping("/selectNoApplyLinksCount")
    @ResponseBody
    public Map<String,Object> selectNoApplyLinksCount() {
        Map<String,Object> map = new HashMap<>();
        int count = linkService.selectNoApplyLinksCount();

        if (count > 0) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }
        map.put("count", count);

        return map;
    }

    /**
     * 添加友情链接
     * 需要考虑是否与某个链接重复
     */
    @RequestMapping("/addLink")
    @ResponseBody
    public Map<String,Object> addLink(Link link) {
        Map<String,Object> map = new HashMap<>();
        /** 返回受影响记录的行数 */
        int count = linkService.addLink(link);

        if (count == 1) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }

        return map;
    }

    /**
     * 根据Id删除友情链接
     */
    @RequestMapping("/deleteLinkById")
    @ResponseBody
    public Map<String,Object> deleteLinkById(@RequestParam("id") Integer id) {
        Map<String,Object> map = new HashMap<>();
        /** 返回受影响记录的行数 */
        int count = linkService.deleteLinkById(id);

        if (count == 1) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }

        return map;
    }
    /**
     * 更新友情链接
     */
    @RequestMapping("/updateLinkById")
    @ResponseBody
    public Map<String,Object> updateLinkById(Link link) {
        Map<String,Object> map = new HashMap<>();
        /** 返回受影响记录的行数 */
        int count = linkService.updateLinkById(link);

        if (count == 1) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }

        return map;
    }

    /**
     * 根据Id查询友情链接
     */
    @RequestMapping("/selectLinksById")
    @ResponseBody
    public Map<String,Object> selectLinksById(@RequestParam("id") Integer id) {
        Map<String,Object> map = new HashMap<>();
        Link link = linkService.selectLinksById(id);

        if (link != null) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }
        map.put("link", link);

        return map;
    }

    /**
     * 根据查询条件查询友情链接
     */
    @RequestMapping("/selectLikeLinksListByPage")
    @ResponseBody
    public Map<String,Object> selectLikeLinksListByPage(
            Link link,
            @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = true, defaultValue = "10") Integer pageSize
    ) {
        /** 封装查询条件 */
        Map<String,Object> map = new HashMap<>();
        /** 0代表查询所有 */
        /** 不能用get方法去获取为null的值与其他值进行比较 */
        if (link.getName() != null && "".equals(link.getName()) != true) {
            map.put("name", link.getName());
        }
        if (link.getStatus() != null && "".equals(link.getStatus()) != true) {
            if (link.getStatus() != 0) {
                map.put("status", link.getStatus());
            }
        }
        if (link.getIsApply() != null && "".equals(link.getIsApply()) != true) {
            map.put("isApply", link.getIsApply());
        }


        /** 开始分页，用 PageHelper 插件封装数据 */
        PageHelper.startPage(page, pageSize);
        List<Link> linkList = linkService.selectLikeLinksListByPage(map);
        PageInfo pageInfo = new PageInfo(linkList);
        Map<String,Object> resultMap = new HashMap<>();

        if (linkList.size() > 0) {
            resultMap.put("status", 200);
        } else {
            resultMap.put("status", 500);
        }
        resultMap.put("pageInfo", pageInfo);

        return resultMap;
    }

    /**
     * 按照状态查询友情链接数量
     */
    @RequestMapping("/selectLinksListByStatus")
    @ResponseBody
    public Map<String,Object> selectLinksListByStatus() {
        Map<String,Object> map = new HashMap<>();
        List<?> list = linkService.selectLinksListByStatus();

        if (list.size() > 0) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }
        map.put("list", list);

        return map;
    }

}
